//
//  CameraViewController.m
//  iPhoneDev01
//
//  Created by smu on 11-7-14.
//  Copyright 2011年 duk. All rights reserved.
//

#import "CameraViewController.h"


@implementation CameraViewController
@synthesize imageView;
@synthesize takePictureButton;
@synthesize selectFromCameraRollButton;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [imageView release];
    [takePictureButton release];
    [selectFromCameraRollButton release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    //检查应用程序所在的设备是否有照相机
    if (![UIImagePickerController isSourceTypeAvailable:
          UIImagePickerControllerSourceTypeCamera]) {
        //隐藏两个照相相关的按钮
        takePictureButton.hidden = YES;
        selectFromCameraRollButton.hidden = YES;
    }
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    self.imageView = nil;
    self.takePictureButton = nil;
    self.selectFromCameraRollButton = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark -
- (IBAction)getCameraPicture:(id)sender
{
    //分配初始化UIImagePickerController实例
    UIImagePickerController *picker = 
    [[UIImagePickerController alloc] init];
    //self赋值给图像选取器委托
    picker.delegate = self;
    //可以在拍摄照片后对他进行编辑
    picker.allowsImageEditing = YES;
    //根据所按下的按钮设置sourceType
    //如果用户按下Take New Picture按钮，则告知选取器允许使用照相机
    //如果用户按下Pick from Camera Roll按钮，则使用UIImagePickerControllerSourceType..
    //在所有的照相机设备上允许用户从当前相册种进行选择图像
    picker.sourceType = (sender == takePictureButton) ?
        UIImagePickerControllerSourceTypeCamera :
        UIImagePickerControllerSourceTypePhotoLibrary;
    //呈现图像选择器并释放实例
    [self presentModalViewController:picker animated:YES];
    [picker release];
}
//允许用户从照片库中取得图片
- (IBAction)selectExistingPicture
{
    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsImageEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentModalViewController:picker animated:YES];
    } else {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Error accessing photo library"
                              message:@"Device does not support a photo library" 
                              delegate:nil
                              cancelButtonTitle:@"Drat!" 
                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}
#pragma mark -
//用户完成使用图片选择器时调用，用户选择一幅图片之后也会调用此方法，无论使用哪种源类型
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    //设置imageView,让它显示返回的图像
    imageView.image = image;
    //让选取器解除自身，以使用户返回到应用程序视图
    [picker dismissModalViewControllerAnimated:YES];
}

//用户取消，不需做其他事情，只关闭图像选取器，否则他会一直在那挡住应用程序视图
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissModalViewControllerAnimated:YES];
}
@end
